#!/usr/bin/env python
#coding=utf-8
from flask import Flask
from .extensions import (db,csrf,jwt,api,cors)

def current_app(config_name=None, modules=None,instance_path=None):
    app = Flask(__name__)
    if config_name=="default" or config_name=="development":
        app.config.from_object('Applications.config.setting.DevelopmentConfig')
    elif config_name=="ProductionConfig":
        app.config.from_object('Applications.config.setting.ProductionConfig')
    #app.config.from_object('Applications.config.logconfig.log')

    app.static_url_path = app.config.get('STATIC_FOLDER')
    app.static_folder=app.root_path + app.static_url_path

    #register_api(app)  # api
    register_blueprints(app)  # 蓝图注册

    #app.static_folder = 'static'

    #configure_handle_error(app)

    configure_extensions(app)#扩展加载
    configure_context_processors(app)#context_processor
    return app


#扩展加载
def configure_extensions(app):
    # Flask-SQLAlchemy
    db.init_app(app)
    # Flask-Cache
    #cache.init_app(app)
    # Flask-WTF CSRF
    csrf.init_app(app)
    jwt.init_app(app)
    api.init_app(app)
    cors.init_app(app)
    # Session-redis
    #flasksess.init_app(app)

    #cache-redis
    #cache.init_app(app)

    #from flask_uploads import configure_uploads,patch_request_class
    #configure_uploads(app,aimages)
    #patch_request_class(app)

# 蓝图注册API
def register_api(app):
    from Applications.routes.api import create_HOME_API_blueprint
    create_HOME_API_blueprint(app, "/api_web_service")


# 蓝图注册
def register_blueprints(app):
    from Applications.routes.web import create_official_blueprint
    #from Applications.routes.admin import create_admin_blueprint
    #create_admin_blueprint(app, "/admin")

    #from Applications.routes.home import create_HOME_blueprint
    #create_HOME_blueprint(app, "/home_web_service")




    #from Applications.routes.api import create_api_blueprint#web_service

    create_official_blueprint(app,"/official")

    #create_api_blueprint(app, "/api")



def configure_context_processors(app):
    @app.context_processor
    def get_admin_context_processor():
        #from .services.AdminMenuService import adminMenusServer
        #return adminmenusMode.get_all()
        return dict(admin_menus=None)



